package com.kly.test.base

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.os.IBinder
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.viewbinding.ViewBinding
import com.kly.module_base.base.BaseCommonActivity
import com.kly.module_base.view.CustomDialog
import com.kly.module_base.base.BaseViewModel
import kotlin.math.abs


abstract class BaseVMActivity<Binding : ViewBinding, ViewModel : BaseViewModel>(inflaterBlock: (LayoutInflater) -> Binding, modelClass: Class<ViewModel>) : BaseCommonActivity<Binding, ViewModel>(inflaterBlock, modelClass) {
    private var y1 = 0f
    private var y2 = 0f

    var progressDialog: CustomDialog? = null

    override fun beforeOnCreate(savedInstanceState: Bundle?) {
        super.beforeOnCreate(savedInstanceState)
        mViewModel?.baseCallBack?.observeForever {
            when (it.first) {
                BaseViewModel.SHOW_PROGRESS -> {
                    if (it.second.isNullOrBlank().not()) {
                        showProgressDialog(context = this, text = it.second)
                    } else {
                        showProgressDialog(this)
                    }
                }
                BaseViewModel.HIDE_PROGRESS -> {
                    hideProgressDialog()
                }
            }
        }
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (ev?.action == MotionEvent.ACTION_DOWN) {
            y1 = ev.y
        }
        if (ev?.action == MotionEvent.ACTION_MOVE) {
            y2 = ev.y
            if (abs(y2 - y1) > 50) {
                val v = currentFocus
                if (isShouldHideInput(v, ev)) {
                    hideSoftInput(v?.windowToken)
                }
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    private fun isShouldHideInput(v: View?, event: MotionEvent): Boolean {
        if (v != null && v is EditText) {
            val l = intArrayOf(0, 0)
            v.getLocationInWindow(l)
            val left = l[0]
            val top = l[1]
            val bottom = top + v.getHeight()
            val right = (left + v.getWidth())
            return !(event.x > left && event.x < right && event.y > top && event.y < bottom)
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditView上，和用户用轨迹球选择其他的焦点
        return false
    }

    private fun hideSoftInput(token: IBinder?) {
        if (token != null) {
            val im: InputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
            im.hideSoftInputFromWindow(
                token,
                InputMethodManager.HIDE_NOT_ALWAYS
            )
        }
    }

    fun showProgressDialog(context: Context?) {
        showProgressDialog(context, false)
    }

    private fun showProgressDialog(context: Context?, cancelable: Boolean) {
        showProgressDialog(context, "请稍候", cancelable)
    }

    fun showProgressDialog(context: Context?, text: String?, cancelable: Boolean = false) {
        if (context != null && !(context as Activity).isDestroyed && progressDialog == null) {
            progressDialog = CustomDialog(context, text, cancelable)
            progressDialog?.show()
        }
    }

    fun hideProgressDialog() {
        if (!isDestroyed) {
            progressDialog?.dismiss()
            progressDialog = null
        }
    }

    override fun onDestroyFrame() {
        super.onDestroyFrame()
        hideProgressDialog()
    }
}